﻿namespace Super.Hmi.Utils.Extensions
{
    /// <summary>
    /// DateTime 扩展
    /// </summary>
    public static class DateTimeExtensions
    {
        /// <summary>
        /// 设置小时
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="hour"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static DateTime? WithHour(this DateTime? dateTime, int hour)
        {
            if (!dateTime.HasValue) return null;
            if (hour < 0 || hour > 23)
                throw new ArgumentOutOfRangeException(nameof(hour), "小时值必须在0-23之间");
            var value = dateTime.Value;
            return new DateTime(
                value.Year, value.Month, value.Day,
                hour,
                value.Minute, value.Second, value.Millisecond,
                value.Kind);
        }
        /// <summary>
        /// 设置分钟
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="minute"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static DateTime? WithMinute(this DateTime? dateTime, int minute)
        {
            if (!dateTime.HasValue) return null;
            if (minute < 0 || minute > 59)
                throw new ArgumentOutOfRangeException(nameof(minute), "分钟值必须在0-59之间");
            var value = dateTime.Value;
            return new DateTime(
                value.Year, value.Month, value.Day,
                value.Hour,
                minute, value.Second, value.Millisecond,
                value.Kind);
        }
        /// <summary>
        /// 设置秒
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static DateTime? WithSecond(this DateTime? dateTime, int second)
        {
            if (!dateTime.HasValue) return null;
            if (second < 0 || second > 59)
                throw new ArgumentOutOfRangeException(nameof(second), "秒值必须在0-59之间");
            var value = dateTime.Value;
            return new DateTime(
                value.Year, value.Month, value.Day,
                value.Hour,
                value.Minute, second, value.Millisecond,
                value.Kind);
        }
    }
}
